<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/13
 * Time: 10:13
 */

namespace app\api\controller;


use app\admin\model\order\OrderQueue;
use app\common\controller\Api;
use app\common\library\ali\rabittmq\Producer;

class Servers extends Api {

    protected $noNeedLogin = ['*'];

    /**
     * 约定时间监听加入队列
     * */
    public function appoint_time_lisen(){
        $order = new \app\admin\model\order\Order();
        //2 小时内
        $where = [
            'pay_status'=>'1',
        ];
        $list = $order->alias('o')
            ->join('fa_order_queue q ','q.order_id = o.order_id')
        ->where($where)
        ->whereTime('appoint_time',['between'=>[strtotime('-2 hours'),date('Y-m-d H:i:s')]])
        ->field('o.order_id,q.id')
        ->select();
        if($list){
            $listArr = collection($list)->toArray();
            $producer =new Producer();
            $orderQueue = new OrderQueue();
            $insertData = [];
            foreach ($listArr as $k=>$v){
                if(!$v['id']) {
                    //是否已加入队列
                    $producer->setQueue('');
                    $producer->setMessage(json_encode(['order_id'=>$v['order_id']]));
                    $insertData[] =
                        [
                            'order_id'=>$v['order_id']
                        ];
                }
            }
            if(!empty($insertData)){
                $orderQueue->insertAll($insertData);
            }
        }

    }
}